package com.carrental.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.vehicle.entity.Image;
import com.carrental.vehicle.mapper.ImageMapper;
import com.carrental.vehicle.service.ImageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 图片服务实现类
 */
@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements ImageService {

    @Override
    public IPage<Image> pageList(Integer current, Integer size, Long categoryId, String name) {
        LambdaQueryWrapper<Image> queryWrapper = new LambdaQueryWrapper<>();
        
        queryWrapper.eq(Image::getCategoryId, categoryId);
        
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Image::getName, name);
        }
        
        queryWrapper.orderByAsc(Image::getSort);
        
        Page<Image> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addImage(Image image) {
        image.setCreateTime(LocalDateTime.now());
        image.setUpdateTime(LocalDateTime.now());
        return this.save(image);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateImage(Image image) {
        image.setUpdateTime(LocalDateTime.now());
        return this.updateById(image);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteImage(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteImage(List<Long> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public List<Image> getImagesByCategoryId(Long categoryId) {
        LambdaQueryWrapper<Image> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Image::getCategoryId, categoryId);
        queryWrapper.orderByAsc(Image::getSort);
        return this.list(queryWrapper);
    }
} 